package com.yunkaweilai.android.function.update;

import android.app.Activity;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yunkaweilai.android.R;

import org.lzh.framework.updatepluginlib.callback.UpdateDownloadCB;
import org.lzh.framework.updatepluginlib.creator.DownloadCreator;
import org.lzh.framework.updatepluginlib.model.Update;

import java.io.File;

/**
 * Created by Administrator on 2017/6/24.
 */

public class DialogDownloadCreator implements DownloadCreator {
    @Override
    public UpdateDownloadCB create(Update update, Activity activity) {

        return new DialogCB(activity);
    }

    private static class DialogCB implements UpdateDownloadCB{
        MaterialDialog dialog;

        DialogCB(Activity activity){
            dialog = new MaterialDialog.Builder(activity)
                    .title("系统升级：")
                    .content("下载数据中...")
                    .progress(false, 100,false)
                    .cancelable(false)
                    .progressIndeterminateStyle(true)
                    .build();
        }

        @Override
        public void onUpdateStart() {
            // 下载开始时的通知回调。运行于主线程
            dialog.show();
        }

        @Override
        public void onUpdateComplete(File file) {
            // 下载完成的回调。运行于主线程
            dialog.dismiss();

        }

        @Override
        public void onUpdateProgress(long current, long total) {
            // 下载过程中的进度信息。在此获取进度信息。运行于主线程
            int progress = (int) (current * 1f / total * 100);
            dialog.setProgress(progress);

        }

        @Override
        public void onUpdateError(int code, String errorMsg) {
            // 下载时出错。运行于主线程
            dialog.dismiss();
        }
    }

}
